<?php

namespace app\store\service\server;

/**
 * 订单导出服务类
 * Class Export
 * @package app\store\service\order
 */
class Export
{
    /**
     * 表格标题
     * @var array
     */
    private $tileArray = [
        '昵称', '姓名', '手机号', '金额', '资金流动类型', '备注', '审核时间'
    ];

    /**
     * 订单导出
     * @param $list
     */
    public function orderList($list)
    {
        // 表格内容
        $dataArray = [];
        foreach ($list as $order) {
            /* @var OrderAddressModel $address */
            $dataArray[] = [
                '昵称' => $this->filterValue($order['nickName']),
                '姓名' => $this->filterValue($order['real_name']),
                '手机号' => $this->filterValue($order['mobile']),
                '金额' => $this->filterValue($order['money']),
                '资金流动类型' => $this->filterValue($order['flow_type']['text']),
                '备注' => $this->filterValue($order['describe']),
                '审核时间' => $this->filterValue($order['create_time']),
            ];
        }
        // 导出csv文件
        $filename = 'capital-' . date('YmdHis');
        return export_excel($filename . '.csv', $this->tileArray, $dataArray);
    }

    /**
     * 批量发货模板
     */
    public function deliveryTpl()
    {
        // 导出csv文件
        $filename = 'delivery-' . date('YmdHis');
        return export_excel($filename . '.csv', ['订单号', '物流单号']);
    }

    /**
     * 格式化商品信息
     * @param $order
     * @return string
     */
//    private function filterGoodsInfo($order)
//    {
//        $content = '';
//        foreach ($order['goods'] as $key => $goods) {
//            $content .= ($key + 1) . ".商品名称：{$goods['goods_name']}\n";
//            !empty($goods['goods_attr']) && $content .= "　商品规格：{$goods['goods_attr']}\n";
//            $content .= "　购买数量：{$goods['total_num']}\n";
//            $content .= "　商品总价：{$goods['total_price']}元\n\n";
//        }
//        return $content;
//    }

    /**
     * 表格值过滤
     * @param $value
     * @return string
     */
    private function filterValue($value)
    {
        return "\t" . $value . "\t";
    }

    /**
     * 日期值过滤
     * @param $value
     * @return string
     */
    private function filterTime($value)
    {
        if (!$value) return '';
        return $this->filterValue(date('Y-m-d H:i:s', $value));
    }

}